﻿using DapperExtensions;
using Logistics.Model;
using Logistics.Model.QueryModel;
using Logistics.Service;
using Logistics.WebApi.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;

namespace Logistics.WebApi.Controllers
{
    [MyAuthority]
    public class ContractCancelController : ApiController
    {
        private ContractCancelService service = new ContractCancelService();
        [HttpPost]
        public ResponseModel GetPageList(ContractCancelQuery query)
        {
            if (query == null)
                query = new ContractCancelQuery();
            var resultmod = new ResponseModel();
            try
            {
                IList<ISort> sort = new List<ISort>
                                    {
                                        Predicates.Sort<wuliu_contract_cancel>(p => p.id,true)
                                    };
                var cancelQuery = new List<IPredicate>();
                if (!string.IsNullOrEmpty(query.wuliu_no))
                    cancelQuery.Add(Predicates.Field<wuliu_contract_cancel>(f => f.wuliu_no, Operator.Eq, query.wuliu_no));
               var companyQuery = new List<IPredicate>();
               if (string.IsNullOrEmpty(query.city_id))
                   companyQuery.Add(Predicates.Field<wuliu_wlcompany_base>(f => f.city_id, Operator.Eq, query.city_id));
                var cancelPredicate = Predicates.Group(GroupOperator.And, cancelQuery.ToArray());
                var wlPredicate = Predicates.Group(GroupOperator.And, cancelQuery.ToArray());

                var list = service.GetPageList(cancelPredicate, wlPredicate, sort, query.page - 1, query.pagesize);
                resultmod.mod = list;
                if (list.TotalNum > 0)
                {
                    resultmod.InitResultState(GetDataStateEnum.Success);
                }
                else
                {
                    resultmod.InitResultState(GetDataStateEnum.Error01);
                }
            }
            catch (Exception ex)
            {
                resultmod.InitResultState(ex);
            }
            return resultmod;
        }

        [HttpGet]
        public ResponseModel GetModel(int id)
        {
            var resultmod = new ResponseModel();
            try
            {
                var model = service.Get(id);
                resultmod.mod = model;
                if (model == null)
                {
                    resultmod.InitResultState(GetDataStateEnum.Error01);
                }
                else {
                    resultmod.InitResultState(GetDataStateEnum.Success);
                }
                
            }
            catch (Exception ex)
            {
                resultmod.InitResultState(ex);
            }
            return resultmod;
        }
    }
}